import 'dart:convert';

import 'package:chewie/chewie.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/constant.dart';

class ArticleDetailPage extends StatefulWidget {
  String article_id;
  ArticleDetailPage({Key key, @required this.article_id}) : super(key: key);

  _ArticleDetailPageState createState() => _ArticleDetailPageState();
}

class _ArticleDetailPageState extends State<ArticleDetailPage> {
  String content = "";
  String title = "";
  String pubtime = "";
  String desc = "";
  String href;
  ChewieController chewieController;
  VideoPlayerController videoPlayerController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetail();
  }

  @override
  void dispose() {
    if (videoPlayerController != null) {
      videoPlayerController.dispose();
      chewieController.dispose();
    }

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: new Scaffold(
        appBar: AppBar(
          title: Text(
            title,
            style: TextStyle(color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))
          ),
          backgroundColor: Colors.white,
          elevation: 1.0,
          brightness: Brightness.light,
          iconTheme: IconThemeData(color: Colors.black87),
        ),
        body: new SingleChildScrollView(
          child: new Container(
            color: Colors.white,
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                chewieController != null
                    ? Container(
                        height: ScreenUtil.getInstance().setHeight(DP_350),
                        child: Chewie(
                          controller: chewieController,
                        ))
                    : Container(),
                Padding(
                  padding: EdgeInsets.fromLTRB(15, 15, 15, 0),
                  child: Text(
                    title,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 18,
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(15, 5, 0, 0),
                  child: Text(
                    desc,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 14,
                    ),
                  ),
                ),
                Container(
                  color: Colors.white,
                  padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                  child: Html(
                      data: content == null ? "" : content,
                      defaultTextStyle: TextStyle(
                          fontSize: ScreenUtil.getInstance()
                              .setSp(NORMAL_FONT_SIZE))),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  getDetail() async {
    Map map = new Map();
    map['article_id'] = widget.article_id;
    FormData data = FormData.fromMap(new Map<String, dynamic>.from(map));
    // map['sign_type'] = SignType.GET_ARTICLE_DETAIL;
    Response<String> response =
        await HttpUtil().post(Api.GetArticleMsgUrl, data: data);
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        // print(userMap['data']['article_msg']['content']
        //     .toString()
        //     .replaceAll("src=\"", "src=\"" + Api.BaseUrl));
        content = userMap['data']['article_msg']['content']
            .toString();
        title = userMap['data']['article_msg']['title'];
        desc = userMap['data']['article_msg']['keywords'];
        href = userMap['data']['article_msg']['href'];
        if (href != null && href != "") {
          videoPlayerController = VideoPlayerController.network(href);
          chewieController = ChewieController(
            videoPlayerController: videoPlayerController,
            aspectRatio: 3 / 2,
            autoPlay: true,
            looping: true,
          );
        }
      });
    }
  }
}
